package com.zys.background.common.config;

import io.swagger.annotations.Api;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;

/**
 * @author zys
 * @since 2022-11-14
 */
@Configuration
@EnableOpenApi
@EnableAutoConfiguration
public class SwaggerConfig {
  @Bean
  public Docket docket(ApiInfo apiInfo) {

    ArrayList<RequestParameter> parameters = new ArrayList<>();
    parameters.add(new RequestParameterBuilder()
        .name("token")
        .description("令牌")
        .required(false)
        .in(ParameterType.HEADER)
        .build()
    );
    return new Docket(DocumentationType.OAS_30).select()
        .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
        .paths(PathSelectors.any())
        .build()
        .globalRequestParameters(parameters)
        .apiInfo(apiInfo);
  }

  @Bean
  public ApiInfo apiInfo() {
    // http://localhost:8080/swagger-ui/
    return new ApiInfoBuilder()
        .title("background system")
        .description("background system api")
        .contact(new Contact("zys", "", "1083734160@qq.com"))
        .version("1.0")
        .build();
  }
}
